package com.bjpowernode.workbench.web.controller;

import com.bjpowernode.commons.contants.Contants;
import com.bjpowernode.commons.domain.ReturnObject;
import com.bjpowernode.commons.utils.DateUtils;
import com.bjpowernode.settings.web.domain.User;
import com.bjpowernode.settings.web.service.impl.UserServiceImpl;
import com.bjpowernode.workbench.web.domain.*;
import com.bjpowernode.workbench.web.service.ClueActivityRelationService;
import com.bjpowernode.workbench.web.service.ClueRemarkService;
import com.bjpowernode.workbench.web.service.impl.*;
import org.junit.AfterClass;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;

@Controller
public class WorkBenchClueController {

    @Autowired
    private UserServiceImpl userService;

    @Autowired
    private DicValueServiceImpl dicValueService;

    @Autowired
    private ClueServiceImpl clueService;

    @Autowired
    private ActivityServiceImpl activityService;

    @Autowired
    private ClueRemarkServiceImpl clueRemarkService;

    @Autowired
    private ClueActivityRelationServiceImpl clueActivityRelationService;

    @RequestMapping("/workbench/clue/index.do")
    public String clueIndex(HttpServletRequest request){
        List<User> userList = userService.getAllUser();
        List<DicVlaue> appellationList = dicValueService.selectDicValueByTypeCode("appellation");
        List<DicVlaue> sourceList = dicValueService.selectDicValueByTypeCode("source");
        List<DicVlaue> clueStateList = dicValueService.selectDicValueByTypeCode("clueState");

        request.setAttribute("userList",userList);
        request.setAttribute("appellationList",appellationList);
        request.setAttribute("sourceList",sourceList);
        request.setAttribute("clueStateList",clueStateList);

        return "workbench/clue/index";
    }

    @ResponseBody
    @RequestMapping("/workbench/clue/index/insertClue.do")
    public Object insertClue(Clue clue,HttpSession session){
        ReturnObject returnObject = new ReturnObject();
        User user = (User) session.getAttribute(Contants.SESSION_USER);
        clue.setId(UUID.randomUUID().toString().replaceAll("-",""));
        clue.setCreateBy(user.getId());
        clue.setCreateTime(DateUtils.formateDateTime(new Date()));
        try {
            int insertCount = clueService.insertClue(clue);
            if (insertCount>0){
                returnObject.setCode(Contants.RETURN_OBJECT_CODE_SUCCESS);
            }else {
                returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return returnObject;
    }


    @ResponseBody
    @RequestMapping("/workbench/clue/index/selectClueForPageByConditions.do")
    public Object selectClueForPageByConditions(Clue clue){
        System.out.println(clue.toString()+"-------------------");
        int pageNo = (clue.getPageNo()-1)*clue.getPageSize();
        clue.setPageNo(pageNo);
        List<Clue> clueList = clueService.selectClueForPageByConditions(clue);
        Map<String,Object> map = new HashMap<>();
        int total = clueService.selectClueCountByCondition(clue);
           map.put("clueList",clueList);
           map.put("total",total);
        return map;
    }


    @ResponseBody
    @RequestMapping("/workbench/clue/index/deleteClueById.do")
    public Object deleteClueById(String[] id){
        ReturnObject returnObject = new ReturnObject();
        try {
            int deleteCount =clueService.deleteClueById(id);
            if (deleteCount<=0){
                returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
                returnObject.setMessage("删除失败");
            }else {
                returnObject.setCode(Contants.RETURN_OBJECT_CODE_SUCCESS);
                returnObject.setMessage("删除成功");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return returnObject;
    }

    @ResponseBody
    @RequestMapping("/workbench/clue/updateClueByClue.do")
    public Object updateClueById(Clue clue,HttpSession session){
        ReturnObject returnObject = new ReturnObject();
        User user = (User) session.getAttribute(Contants.SESSION_USER);
        clue.setEditBy(user.getId());
        clue.setEditTime(DateUtils.formateDateTime(new Date()));
        try {
            int updateCount = clueService.updateClueByClue(clue);
            if (updateCount<=0){
                returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
            }else {
                returnObject.setCode(Contants.RETURN_OBJECT_CODE_SUCCESS);
                returnObject.setRetData(updateCount);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return returnObject;
    }

    @ResponseBody
    @RequestMapping("/workbench/clue/selectClueById.do")
    public Object selectClueById(String id){
        ReturnObject returnObject = new ReturnObject();
        Clue clue = clueService.selectClueById(id);
        if (clue!=null){
            returnObject.setCode(Contants.RETURN_OBJECT_CODE_SUCCESS);
            returnObject.setRetData(clue);
        }else {
            returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
        }
        return returnObject;
    }

    @ResponseBody
    @RequestMapping("/workbench/clue/selectValueByTypeCode.do")
    public Object selectValueByTypeCode(String typeCode){
        List<DicVlaue> clueStateList = dicValueService.selectDicValueByTypeCode("clueState");
        List<DicVlaue> clueStageList = dicValueService.selectDicValueByTypeCode("stage");
        List<DicVlaue> returnStateList = dicValueService.selectDicValueByTypeCode("returnState");
        List<DicVlaue> sourceList = dicValueService.selectDicValueByTypeCode("source");
        List<DicVlaue> returnPriorityList = dicValueService.selectDicValueByTypeCode("returnPriority");
        List<DicVlaue> appellationList = dicValueService.selectDicValueByTypeCode("appellation");
        List<DicVlaue> transactionTypeList = dicValueService.selectDicValueByTypeCode("transactionType");

        Map<String,Object> map = new HashMap<>();
        map.put("clueStateList",clueStateList);
        map.put("clueStageList",clueStageList);
        map.put("returnPriorityList",returnPriorityList);
        map.put("returnStateList",returnStateList);
        map.put("sourceList",sourceList);
        map.put("appellationList",appellationList);
        map.put("transactionTypeList",transactionTypeList);
        return map;

    }

    @RequestMapping("/workbench/clue/detail.do")
    public String clueDetail(String id,HttpServletRequest request){
        Clue clue = clueService.selectClueById(id);
        List<Activity> activityList = activityService.selectActivityForDetailByClueId(id);
        List<ClueRemark> clueRemarkList = clueRemarkService.selectClueRemarkForDetailByClueId(id);
        request.setAttribute("clue",clue);
        request.setAttribute("activityList",activityList);
        request.setAttribute("clueRemarkList",clueRemarkList);
        return "workbench/clue/detail";
    }

    @ResponseBody
    @RequestMapping("/workbench/clue/insertClueRemark.do")
    public Object insertClueRemark(ClueRemark clueRemark,HttpSession session){
        User user = (User) session.getAttribute(Contants.SESSION_USER);
        ReturnObject returnObject = new ReturnObject();
        String id = UUID.randomUUID().toString().replaceAll("-","");
        String time = DateUtils.formateDateTime(new Date());
        clueRemark.setId(id);
        clueRemark.setCreateBy(user.getId());
        clueRemark.setCreateTime(time);
        clueRemark.setEditFlag("0");
        try{
            int insertCount = clueRemarkService.insertClueRemark(clueRemark);
            if (insertCount<=0){
                returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
                returnObject.setMessage("添加失败");
            }else {
                returnObject.setCode(Contants.RETURN_OBJECT_CODE_SUCCESS);
                returnObject.setRetData(clueRemark);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return returnObject;
    }

    @ResponseBody
    @RequestMapping("/workbench/clue/selectActivityForDetailByNameAndClueId.do")
    public Object selectActivityForDetailByNameAndClueId(String activityName,String clueId){
        Map<String,String> map = new HashMap<>();
        map.put("activityName",activityName);
        map.put("clueId",clueId);
        List<Activity> activityList = activityService.selectActivityForDetailByNameAndClueId(map);
        ReturnObject returnObject = new ReturnObject();
        if (activityList==null){
            returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
        }else {
            returnObject.setCode(Contants.RETURN_OBJECT_CODE_SUCCESS);
            returnObject.setRetData(activityList);
        }
        return returnObject;
    }

    @ResponseBody
    @RequestMapping("workbench/clue/insertClueActivityRelationByList.do")
    public Object insertClueActivityRelationByList(String[] activityId,String clueId){
        ReturnObject returnObject = new ReturnObject();
        List<ClueActivityRelation> clueActivityRelationList = new ArrayList<>();
        ClueActivityRelation clueActivityRelation = null;
        List<Activity> activityList = activityService.selectActivityForDetailByActivityIds(activityId);
        for(String ai:activityId){
            clueActivityRelation = new ClueActivityRelation();
            clueActivityRelation.setId(UUID.randomUUID().toString().replaceAll("-",""));
            clueActivityRelation.setActivityId(ai);
            clueActivityRelation.setClueId(clueId);
            clueActivityRelationList.add(clueActivityRelation);
        }
       try{
           int insertCount = clueActivityRelationService.insertClueActivityRelationByList(clueActivityRelationList);
           if (insertCount>0){
               returnObject.setCode(Contants.RETURN_OBJECT_CODE_SUCCESS);
               returnObject.setRetData(activityList);
           }else {
               returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
           }
       }catch (Exception e){
           e.printStackTrace();
       }
        return returnObject;
    }

    @ResponseBody
    @RequestMapping("/workbench/clue/selectActivityForDetailByActivityIds.do")
    public Object selectActivityForDetailByActivityIds(String[] id){
        List<Activity> activityList = activityService.selectActivityForDetailByActivityIds(id);
        ReturnObject returnObject = new ReturnObject();
        if (activityList!=null){
            returnObject.setCode(Contants.RETURN_OBJECT_CODE_SUCCESS);
            returnObject.setRetData(activityList);
        }else {
            returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
        }

        return returnObject;
    }

    @ResponseBody
    @RequestMapping("/workbench/clue/deleteClueActivityByClueActivityRelation.do")
    public Object deleteClueActivityByClueActivityRelation(ClueActivityRelation clueActivityRelation){
        ReturnObject returnObject = new ReturnObject();
        try{
            int delCount = clueActivityRelationService.deleteClueActivityByClueActivityRelation(clueActivityRelation);
            if (delCount>0){
                returnObject.setCode(Contants.RETURN_OBJECT_CODE_SUCCESS);
            }else {
                returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return returnObject;
    }

    @RequestMapping("/workbench/clue/toConvert.do")
    public String toConvert(String id,HttpServletRequest request){
        Clue clue = clueService.selectClueById(id);
        List<DicVlaue> dicVlaueList = dicValueService.selectDicValueByTypeCode("stage");
        request.setAttribute("clue",clue);
        request.setAttribute("dicVlaueList",dicVlaueList);
        return "workbench/clue/convert";
    }


    @ResponseBody
    @RequestMapping("workbench/clue/selectActivityForConvertByNameAndClueId.do")
    public Object selectActivityForConvertByNameAndClueId(String activityName,String clueId){
        Map<String,Object> map = new HashMap<>();
        map.put("activityName",activityName);
        map.put("clueId",clueId);
        List<Activity> activityList = activityService.selectActivityForConvertByNameAndClueId(map);
        return activityList;
    }

    @ResponseBody
    @RequestMapping("/workbench/clue/convertClue.do")
    public Object converClue(String clueId,String money,String name,String expectedDate,String stage,String activityId,String isCreateTran,HttpSession session){
        Map<String,Object> map = new HashMap<>();
        User user = (User)session.getAttribute(Contants.SESSION_USER);
        map.put("clueId",clueId);
        map.put("money",money);
        map.put("name",name);
        map.put("expectedDate",expectedDate);
        map.put("stage",stage);
        map.put("activityId",activityId);
        map.put("isCreateTran",isCreateTran);
        map.put(Contants.SESSION_USER,user);
        ReturnObject returnObject = new ReturnObject();
        try{
            clueService.saveConvert(map);
            returnObject.setCode(Contants.RETURN_OBJECT_CODE_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
        }
        return returnObject;
    }

    }
